package com.aidong.service;

/**
 * Created by john on 16-11-2.
 */
import com.aidong.cache.ItemCache;
import com.aidong.common.JobConsts;
import com.aidong.dao.wills.MemberExpiredAnalysisDao;
import com.aidong.model.wills.MemberExpiredAnalysis;
import com.aidong.util.DateUtil;
import com.aidong.util.ExportUtil;
import com.aidong.util.FileCheckUtil;
import com.aidong.util.ReportUtil;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

@Service
public class MemberExpiredReportService extends BaseJobService {
    protected Logger log = Logger.getLogger(MemberExpiredReportService.class);
    @Resource
    MemberExpiredAnalysisDao memberExpiredAnalysisDao;
    @Resource
    ReportUtil reportUtil;
    @Resource
    protected ItemCache cache;

    public MemberExpiredReportService() {
    }

    public synchronized void doJob() {
        Thread th1 = new Thread() {
            public void run() {
                try {
                    try {
                        MemberExpiredReportService.this.doMemberExpiredReport();
                    } catch (Exception var5) {
                        var5.printStackTrace();
                    }

                } finally {
                    ;
                }
            }
        };
        th1.start();
    }

    private void doMemberExpiredReport() throws Exception {
        try {
            Timestamp e = DateUtil.getCurrentTimestamp();
            String cur = DateUtil.formatDate(e, DateUtil.DATE_PATTERN_YYYYMMDD);
            String curDate = DateUtil.addDays(cur, -1);
            Calendar ca = Calendar.getInstance();
            ca.setTime(DateUtil.formatDate(curDate));
            int year = ca.get(1);
            int month = ca.get(2) + 1;
            HashMap commonData = new HashMap();
            commonData.put("year", Integer.valueOf(year));
            commonData.put("month", Integer.valueOf(month));
            Map data = this.createAllData();
            data.putAll(commonData);
            String fileName = curDate + "_" + JobConsts.TEMPLATE_NAME_MEMBER_EXPIRED_REPORT + JobConsts.EXCEL_SUFFIX;
            String fileChName = curDate + "_" + JobConsts.CH_NAME_MEMBER_EXPIRED_REPORT + JobConsts.EXCEL_SUFFIX;
            String filePath = this.getDownloadPath() + curDate + "\\" + JobConsts.TEMPLATE_NAME_MEMBER_EXPIRED_REPORT;
            String fileDbPath = curDate + "\\" + JobConsts.TEMPLATE_NAME_MEMBER_EXPIRED_REPORT;
            FileCheckUtil.checkFilePath(filePath);
            ExportUtil.exportExcel(filePath, fileName, this.getTemplatePath(), JobConsts.TEMPLATE_NAME_MEMBER_EXPIRED_REPORT + JobConsts.EXCEL_SUFFIX, data);
            this.reportUtil.addReportFile("05", fileName, fileChName, fileDbPath, curDate, "ALL");
        } catch (Exception var13) {
            var13.printStackTrace();
            this.log.error("生成剩余有效会籍人数表失败");
            return;
        }

        this.log.info("生成剩余有效会籍人数表成功");
    }

    private Map<String, Object> createAllData() {
        HashMap data = new HashMap();
        List allLst = this.memberExpiredAnalysisDao.getAllMemberExpired();
        int i = 0;
        Iterator i$ = allLst.iterator();

        while(i$.hasNext()) {
            MemberExpiredAnalysis po = (MemberExpiredAnalysis)i$.next();
            ++i;
            po.setNo(i);
            po.setShopName(this.cache.getValue("shopName", po.getShopCode()));
        }

        if(allLst.size() == 0) {
            allLst.add(new MemberExpiredAnalysis());
        }

        data.put("mbLst", allLst);
        return data;
    }
}
